<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5                                                          |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | This source file is subject to version 3.00 of the PHP License,        |
// | that is available at http://www.php.net/license/3_0.txt.               |
// | If you did not receive a copy of the PHP license and are unable to     |
// | obtain it through the world-wide-web, please send a note to            |
// | license@php.net so we can mail you a copy immediately.                 |
// +------------------------------------------------------------------------+
// | Author:                                                                |
// +------------------------------------------------------------------------+
//
// $Id$
//


/**
 * @author       mk
 */
class PhpView {
    protected
        $theme = 'default',
        $layoutPath = null,
        $var = array();
    public $pageTitle = '';
    public $webroot = '';
    public $layout = 'layout';
    
    /**
     * 设置布局路径
     *
     * @param string $layoutPath
     */
    public function setLayoutPath($layoutPath) {
        $this->layoutPath = $layoutPath;
    }
    
    public function set($var, $value) {
        $this->var[$var] = $value;
    }
    
    /**
     * @param    string $template    
     * @return   void
     */
    public function render($template) {
        foreach ($this->var as $key => $var) {
            $$key = $var;
        }
        
        ob_start();
        include($template);
        
        $content_for_layout = ob_get_clean();
        if ($this->layout) {
            //$layout = ROOT.'/theme/'.$this->theme.'/'.$this->layout.'.php';
            $this->renderLayout($content_for_layout);
        } else {
            echo $content_for_layout;
        }
    }
    
    protected function renderLayout($content_for_layout) {
        !empty($this->pageTitle) ? $pageTitle = $this->pageTitle : $pageTitle = '';
        if (empty($this->layoutPath)) {
            $layout = ROOT.'/theme/'.$this->theme.'/'.$this->layout.'.php';
        } else {
            $layout = $this->layoutPath.'/'.$this->layout.'.php';
        }
        include($layout);
    }
    
    protected function _render($template) {
        ob_start();
    }
}

